home *** CD-ROM | disk | FTP | other *** search
/ Whiteline: Alpha / Whiteline Alpha.iso / linux / atari / source / source.lzh / atari-linux-0.01pl3 / drivers / scsi / sd.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-05  |  27.7 KB  |  987 lines

  1. /*
  2.  *    sd.c Copyright (C) 1992 Drew Eckhardt
  3.  *    Linux scsi disk driver by
  4.  *        Drew Eckhardt
  5.  *
  6.  *    <drew@colorado.edu>
  7.  *
  8.  *     Modified by Eric Youngdale eric@tantalus.nrl.navy.mil to
  9.  *     add scatter-gather, multiple outstanding request, and other
  10.  *     enhancements.
  11.  */
  12.  
  13. #include <linux/fs.h>
  14. #include <linux/kernel.h>
  15. #include <linux/sched.h>
  16. #include <linux/string.h>
  17. #include <linux/errno.h>
  18. #include <asm/system.h>
  19.  
  20. #define MAJOR_NR SCSI_DISK_MAJOR
  21. #include "../block/blk.h"
  22. #include "scsi.h"
  23. #include "hosts.h"
  24. #include "sd.h"
  25. #include "scsi_ioctl.h"
  26. #include "constants.h"
  27.  
  28. #include <linux/genhd.h>
  29.  
  30. /*
  31. static const char RCSid[] = "$Header:";
  32. */
  33.  
  34. #define MAX_RETRIES 5
  35.  
  36. /*
  37.  *    Time out in seconds
  38.  */
  39.  
  40. #define SD_TIMEOUT 300
  41.  
  42. struct hd_struct * sd;
  43.  
  44. int NR_SD=0;
  45. int MAX_SD=0;
  46. Scsi_Disk * rscsi_disks;
  47. static int * sd_sizes;
  48. static int * sd_blocksizes;
  49.  
  50. extern int sd_ioctl(struct inode *, struct file *, unsigned int, unsigned long);
  51.  
  52. static sd_init_onedisk(int);
  53.  
  54. static void requeue_sd_request (Scsi_Cmnd * SCpnt);
  55.  
  56. static int sd_open(struct inode * inode, struct file * filp)
  57. {
  58.     int target;
  59.     target =  DEVICE_NR(MINOR(inode->i_rdev));
  60.  
  61.     if(target >= NR_SD || !rscsi_disks[target].device)
  62.       return -ENODEV;   /* No such device */
  63.  
  64. /* Make sure that only one process can do a check_change_disk at one time.
  65.  This is also used to lock out further access when the partition table is being re-read. */
  66.  
  67.     while (rscsi_disks[target].device->busy);
  68.  
  69.     if(rscsi_disks[target].device->removable) {
  70.       check_disk_change(inode->i_rdev);
  71.  
  72.       if(!rscsi_disks[target].device->access_count)
  73.         sd_ioctl(inode, NULL, SCSI_IOCTL_DOORLOCK, 0);
  74.     };
  75.     rscsi_disks[target].device->access_count++;
  76.     return 0;
  77. }
  78.  
  79. static void sd_release(struct inode * inode, struct file * file)
  80. {
  81.     int target;
  82.     sync_dev(inode->i_rdev);
  83.  
  84.     target =  DEVICE_NR(MINOR(inode->i_rdev));
  85.  
  86.     rscsi_disks[target].device->access_count--;
  87.  
  88.     if(rscsi_disks[target].device->removable) {
  89.       if(!rscsi_disks[target].device->access_count)
  90.         sd_ioctl(inode, NULL, SCSI_IOCTL_DOORUNLOCK, 0);
  91.     };
  92. }
  93.  
  94. static void sd_geninit(void);
  95.  
  96. static struct file_operations sd_fops = {
  97.     NULL,            /* lseek - default */
  98.     block_read,        /* read - general block-dev read */
  99.     block_write,        /* write - general block-dev write */
  100.     NULL,            /* readdir - bad */
  101.     NULL,            /* select */
  102.     sd_ioctl,        /* ioctl */
  103.     NULL,            /* mmap */
  104.     sd_open,        /* open code */
  105.     sd_release,        /* release */
  106.     block_fsync        /* fsync */
  107. };
  108.  
  109. static struct gendisk sd_gendisk = {
  110.     MAJOR_NR,        /* Major number */
  111.     "sd",           /* Major name */
  112.     4,        /* Bits to shift to get real from partition */
  113.     1 << 4,     /* Number of partitions per real */
  114.     0,        /* maximum number of real */
  115.     sd_geninit,    /* init function */
  116.     NULL,        /* hd struct */
  117.     NULL,    /* block sizes */
  118.     0,        /* number */
  119.     NULL,    /* internal */
  120.     NULL        /* next */
  121. };
  122.  
  123. static void sd_geninit (void)
  124. {
  125.     int i;
  126.  
  127.     for (i = 0; i < NR_SD; ++i)
  128.         sd[i << 4].nr_sects = rscsi_disks[i].capacity;
  129.     sd_gendisk.nr_real = NR_SD;
  130. }
  131.  
  132. /*
  133.     rw_intr is the interrupt routine for the device driver.  It will
  134.     be notified on the end of a SCSI read / write, and
  135.     will take on of several actions based on success or failure.
  136. */
  137.  
  138. static void rw_intr (Scsi_Cmnd *SCpnt)
  139. {
  140.   int result = SCpnt->result;
  141.   int this_count = SCpnt->bufflen >> 9;
  142.  
  143. #ifdef DEBUG
  144.   printk("sd%d : rw_intr(%d, %d)\n", MINOR(SCpnt->request.dev), SCpnt->host->host_no, result);
  145. #endif
  146.  
  147. /*
  148.   First case : we assume that the command succeeded.  One of two things will
  149.   happen here.    Either we will be finished, or there will be more
  150.   sectors that we were unable to read last time.
  151. */
  152.  
  153.   if (!result) {
  154.  
  155. #ifdef DEBUG
  156.     printk("sd%d : %d sectors remain.\n", MINOR(SCpnt->request.dev), SCpnt->request.nr_sectors);
  157.     printk("use_sg is %d\n ",SCpnt->use_sg);
  158. #endif
  159.     if (SCpnt->use_sg) {
  160.       struct scatterlist * sgpnt;
  161.       int i;
  162.       sgpnt = (struct scatterlist *) SCpnt->buffer;
  163.       for(i=0; i<SCpnt->use_sg; i++) {
  164. #ifdef DEBUG
  165.     printk(":%x %x %d\n",sgpnt[i].alt_address, sgpnt[i].address, sgpnt[i].length);
  166. #endif
  167.     if (sgpnt[i].alt_address) {
  168.       if (SCpnt->request.cmd == READ)
  169.         memcpy(sgpnt[i].alt_address, sgpnt[i].address, sgpnt[i].length);
  170.       scsi_free(sgpnt[i].address, sgpnt[i].length);
  171.     };
  172.       };
  173.       scsi_free(SCpnt->buffer, SCpnt->sglist_len);  /* Free list of scatter-gather pointers */
  174.     } else {
  175.       if (SCpnt->buffer != SCpnt->request.buffer) {
  176. #ifdef DEBUG
  177.     printk("nosg: %x %x %d\n",SCpnt->request.buffer, SCpnt->buffer,
  178.            SCpnt->bufflen);
  179. #endif
  180.       if (SCpnt->request.cmd == READ)
  181.         memcpy(SCpnt->request.buffer, SCpnt->buffer,
  182.            SCpnt->bufflen);
  183.       scsi_free(SCpnt->buffer, SCpnt->bufflen);
  184.       };
  185.     };
  186. /*
  187.  *    If multiple sectors are requested in one buffer, then
  188.  *    they will have been finished off by the first command.    If
  189.  *    not, then we have a multi-buffer command.
  190.  */
  191.     if (SCpnt->request.nr_sectors > this_count)
  192.       {
  193.     SCpnt->request.errors = 0;
  194.  
  195.     if (!SCpnt->request.bh)
  196.       {
  197. #ifdef DEBUG
  198.         printk("sd%d : handling page request, no buffer\n",
  199.            MINOR(SCpnt->request.dev));
  200. #endif
  201. /*
  202.   The SCpnt->request.nr_sectors field is always done in 512 byte sectors,
  203.   even if this really isn't the case.
  204. */
  205.         panic("sd.c: linked page request. (%lx %x)",
  206.           SCpnt->request.sector, this_count);
  207.       }
  208.       }
  209.     end_scsi_request(SCpnt, 1, this_count);
  210.     requeue_sd_request(SCpnt);
  211.     return;
  212.   }
  213.  
  214. /* Free up any indirection buffers we allocated for DMA purposes. */
  215.     if (SCpnt->use_sg) {
  216.       struct scatterlist * sgpnt;
  217.       int i;
  218.       sgpnt = (struct scatterlist *) SCpnt->buffer;
  219.       for(i=0; i<SCpnt->use_sg; i++) {
  220. #ifdef DEBUG
  221.     printk("err: %x %x %d\n",SCpnt->request.buffer, SCpnt->buffer,
  222.            SCpnt->bufflen);
  223. #endif
  224.     if (sgpnt[i].alt_address) {
  225.       scsi_free(sgpnt[i].address, sgpnt[i].length);
  226.     };
  227.       };
  228.       scsi_free(SCpnt->buffer, SCpnt->sglist_len);  /* Free list of scatter-gather pointers */
  229.     } else {
  230. #ifdef DEBUG
  231.       printk("nosgerr: %x %x %d\n",SCpnt->request.buffer, SCpnt->buffer,
  232.            SCpnt->bufflen);
  233. #endif
  234.       if (SCpnt->buffer != SCpnt->request.buffer)
  235.     scsi_free(SCpnt->buffer, SCpnt->bufflen);
  236.     };
  237.  
  238. /*
  239.     Now, if we were good little boys and girls, Santa left us a request
  240.     sense buffer.  We can extract information from this, so we
  241.     can choose a block to remap, etc.
  242. */
  243.  
  244.     if (driver_byte(result) != 0) {
  245.       if (sugestion(result) == SUGGEST_REMAP) {
  246. #ifdef REMAP
  247. /*
  248.     Not yet implemented.  A read will fail after being remapped,
  249.     a write will call the strategy routine again.
  250. */
  251.         if rscsi_disks[DEVICE_NR(SCpnt->request.dev)].remap
  252.           {
  253.         result = 0;
  254.           }
  255.         else
  256.  
  257. #endif
  258.         }
  259.  
  260.       if ((SCpnt->sense_buffer[0] & 0x7f) == 0x70) {
  261.         if ((SCpnt->sense_buffer[2] & 0xf) == UNIT_ATTENTION) {
  262.           if(rscsi_disks[DEVICE_NR(SCpnt->request.dev)].device->removable) {
  263.           /* detected disc change.    set a bit and quietly refuse    */
  264.           /* further access.                    */
  265.  
  266.         rscsi_disks[DEVICE_NR(SCpnt->request.dev)].device->changed = 1;
  267.         end_scsi_request(SCpnt, 0, this_count);
  268.         requeue_sd_request(SCpnt);
  269.         return;
  270.           }
  271.         }
  272.       }
  273.  
  274.  
  275. /*    If we had an ILLEGAL REQUEST returned, then we may have
  276. performed an unsupported command.  The only thing this should be would
  277. be a ten byte read where only a six byte read was supportted.  Also,
  278. on a system where READ CAPACITY failed, we mave have read past the end
  279. of the    disk.
  280. */
  281.  
  282.       if (SCpnt->sense_buffer[2] == ILLEGAL_REQUEST) {
  283.         if (rscsi_disks[DEVICE_NR(SCpnt->request.dev)].ten) {
  284.           rscsi_disks[DEVICE_NR(SCpnt->request.dev)].ten = 0;
  285.           requeue_sd_request(SCpnt);
  286.           result = 0;
  287.         } else {
  288.         }
  289.       }
  290.     }  /* driver byte != 0 */
  291.     if (result) {
  292.         printk("SCSI disk error : host %d id %d lun %d return code = %x\n",
  293.                rscsi_disks[DEVICE_NR(SCpnt->request.dev)].device->host->host_no,
  294.                rscsi_disks[DEVICE_NR(SCpnt->request.dev)].device->id,
  295.                rscsi_disks[DEVICE_NR(SCpnt->request.dev)].device->lun, result);
  296.  
  297.         if (driver_byte(result) & DRIVER_SENSE)
  298.             print_sense("sd", SCpnt);
  299.         end_scsi_request(SCpnt, 0, SCpnt->request.current_nr_sectors);
  300.         requeue_sd_request(SCpnt);
  301.         return;
  302.     }
  303. }
  304.  
  305. /*
  306.     requeue_sd_request() is the request handler function for the sd driver.
  307.     Its function in life is to take block device requests, and translate
  308.     them to SCSI commands.
  309. */
  310.  
  311. static void do_sd_request (void)
  312. {
  313.   Scsi_Cmnd * SCpnt = NULL;
  314.   struct request * req = NULL;
  315.   int flag = 0;
  316.   unsigned long flags;
  317.  
  318.   while (1==1){
  319.     save_flags (flags);
  320.     cli();
  321.     if (CURRENT != NULL && CURRENT->dev == -1) {
  322.       restore_flags (flags);
  323.       return;
  324.     };
  325.  
  326.     INIT_SCSI_REQUEST;
  327.  
  328.  
  329. /* We have to be careful here.    allocate_device will get a free pointer, but
  330.    there is no guarantee that it is queueable.    In normal usage, we want to
  331.    call this, because other types of devices may have the host all tied up,
  332.    and we want to make sure that we have at least one request pending for this
  333.    type of device.   We can also come through here while servicing an
  334.    interrupt, because of the need to start another command.  If we call
  335.    allocate_device more than once, then the system can wedge if the command
  336.    is not queueable.  The request_queueable function is safe because it checks
  337.    to make sure that the host is able to take another command before it returns
  338.    a pointer.  */
  339.  
  340.     if (flag++ == 0)
  341.       SCpnt = allocate_device(&CURRENT,
  342.                   rscsi_disks[DEVICE_NR(MINOR(CURRENT->dev))].device->index, 0);
  343.     else SCpnt = NULL;
  344.     restore_flags (flags);
  345.  
  346. /* This is a performance enhancement.  We dig down into the request list and
  347.    try and find a queueable request (i.e. device not busy, and host able to
  348.    accept another command.  If we find one, then we queue it. This can
  349.    make a big difference on systems with more than one disk drive.  We want
  350.    to have the interrupts off when monkeying with the request list, because
  351.    otherwise the kernel might try and slip in a request inbetween somewhere. */
  352.  
  353.     if (!SCpnt && NR_SD > 1){
  354.       struct request *req1;
  355.       req1 = NULL;
  356.       save_flags (flags);
  357.       cli();
  358.       req = CURRENT;
  359.       while(req){
  360.     SCpnt = request_queueable(req,
  361.                   rscsi_disks[DEVICE_NR(MINOR(req->dev))].device->index);
  362.     if(SCpnt) break;
  363.     req1 = req;
  364.     req = req->next;
  365.       };
  366.       if (SCpnt && req->dev == -1) {
  367.     if (req == CURRENT)
  368.       CURRENT = CURRENT->next;
  369.     else
  370.       req1->next = req->next;
  371.       };
  372.       restore_flags(flags);
  373.     };
  374.  
  375.     if (!SCpnt) return; /* Could not find anything to do */
  376.  
  377.     wake_up(&wait_for_request);
  378.  
  379.     /* Queue command */
  380.     requeue_sd_request(SCpnt);
  381.   };  /* While */
  382. }
  383.  
  384. static void requeue_sd_request (Scsi_Cmnd * SCpnt)
  385. {
  386.     int dev, block, this_count;
  387.     unsigned char cmd[10];
  388.     char * buff;
  389.  
  390. repeat:
  391.  
  392.     if(SCpnt->request.dev <= 0) {
  393.       do_sd_request();
  394.       return;
  395.     }
  396.  
  397.     dev =  MINOR(SCpnt->request.dev);
  398.     block = SCpnt->request.sector;
  399.     this_count = 0;
  400.  
  401. #ifdef DEBUG
  402.     printk("Doing sd request, dev = %d, block = %d\n", dev, block);
  403. #endif
  404.  
  405.     if (dev >= (NR_SD << 4) || block + SCpnt->request.nr_sectors > sd[dev].nr_sects)
  406.         {
  407.         end_scsi_request(SCpnt, 0, SCpnt->request.nr_sectors);
  408.         goto repeat;
  409.         }
  410.  
  411.     block += sd[dev].start_sect;
  412.     dev = DEVICE_NR(dev);
  413.  
  414.     if (rscsi_disks[dev].device->changed)
  415.         {
  416. /*
  417.  * quietly refuse to do anything to a changed disc until the changed bit has been reset
  418.  */
  419.         /* printk("SCSI disk has been changed.  Prohibiting further I/O.\n");   */
  420.         end_scsi_request(SCpnt, 0, SCpnt->request.nr_sectors);
  421.         goto repeat;
  422.         }
  423.  
  424. #ifdef DEBUG
  425.     printk("sd%d : real dev = /dev/sd%d, block = %d\n", MINOR(SCpnt->request.dev), dev, block);
  426. #endif
  427.  
  428.     switch (SCpnt->request.cmd)
  429.         {
  430.         case WRITE :
  431.             if (!rscsi_disks[dev].device->writeable)
  432.                 {
  433.                 end_scsi_request(SCpnt, 0, SCpnt->request.nr_sectors);
  434.                 goto repeat;
  435.                 }
  436.             cmd[0] = WRITE_6;
  437.             break;
  438.         case READ :
  439.             cmd[0] = READ_6;
  440.             break;
  441.         default :
  442.             panic ("Unknown sd command %d\n", SCpnt->request.cmd);
  443.               }
  444.  
  445.     SCpnt->this_count = 0;
  446.  
  447.     if (!SCpnt->request.bh ||
  448.         (SCpnt->request.nr_sectors == SCpnt->request.current_nr_sectors)) {
  449.  
  450.       /* case of page request (i.e. raw device), or unlinked buffer */
  451.       this_count = SCpnt->request.nr_sectors;
  452.       buff = SCpnt->request.buffer;
  453.       SCpnt->use_sg = 0;
  454.  
  455.     } else if (SCpnt->host->sg_tablesize == 0 ||
  456.            (need_isa_buffer &&
  457.             dma_free_sectors < 10)) {
  458.  
  459.       /* Case of host adapter that cannot scatter-gather.  We also
  460.        come here if we are running low on DMA buffer memory.  We set
  461.        a threshold higher than that we would need for this request so
  462.        we leave room for other requests.  Even though we would not need
  463.        it all, we need to be conservative, because if we run low enough
  464.        we have no choice but to panic. */
  465.  
  466.       if (SCpnt->host->sg_tablesize != 0 &&
  467.           need_isa_buffer &&
  468.           dma_free_sectors < 10)
  469.         printk("Warning: SCSI DMA buffer space running low.  Using non scatter-gather I/O.\n");
  470.  
  471.       this_count = SCpnt->request.current_nr_sectors;
  472.       buff = SCpnt->request.buffer;
  473.       SCpnt->use_sg = 0;
  474.  
  475.     } else {
  476.  
  477.       /* Scatter-gather capable host adapter */
  478.       struct buffer_head * bh;
  479.       struct scatterlist * sgpnt;
  480.       int count, this_count_max;
  481.       bh = SCpnt->request.bh;
  482.       this_count = 0;
  483.       this_count_max = (rscsi_disks[dev].ten ? 0xffff : 0xff);
  484.       count = 0;
  485.       while(bh && count < SCpnt->host->sg_tablesize) {
  486.         if ((this_count + (bh->b_size >> 9)) > this_count_max) break;
  487.         this_count += (bh->b_size >> 9);
  488.         count++;
  489.         bh = bh->b_reqnext;
  490.       };
  491.       SCpnt->use_sg = count;  /* Number of chains */
  492.       count = 512;/* scsi_malloc can only allocate in chunks of 512 bytes*/
  493.       while( count < (SCpnt->use_sg * sizeof(struct scatterlist)))
  494.         count = count << 1;
  495.       SCpnt->sglist_len = count;
  496.       sgpnt = (struct scatterlist * ) scsi_malloc(count);
  497.       if (!sgpnt) {
  498.         printk("Warning - running *really* short on DMA buffers\n");
  499.         SCpnt->use_sg = 0;    /* No memory left - bail out */
  500.         this_count = SCpnt->request.current_nr_sectors;
  501.         buff = SCpnt->request.buffer;
  502.       } else {
  503.         buff = (char *) sgpnt;
  504.         count = 0;
  505.         bh = SCpnt->request.bh;
  506.         for(count = 0, bh = SCpnt->request.bh; count < SCpnt->use_sg;
  507.         count++, bh = bh->b_reqnext) {
  508.           sgpnt[count].address = bh->b_data;
  509.           sgpnt[count].alt_address = NULL;
  510.           sgpnt[count].length = bh->b_size;
  511.           if (((int) sgpnt[count].address) + sgpnt[count].length >
  512.           ISA_DMA_THRESHOLD & (SCpnt->host->unchecked_isa_dma)) {
  513.         sgpnt[count].alt_address = sgpnt[count].address;
  514.         /* We try and avoid exhausting the DMA pool, since it is easier
  515.            to control usage here.  In other places we might have a more
  516.            pressing need, and we would be screwed if we ran out */
  517.         if(dma_free_sectors < (bh->b_size >> 9) + 5) {
  518.           sgpnt[count].address = NULL;
  519.         } else {
  520.           sgpnt[count].address = (char *) scsi_malloc(sgpnt[count].length);
  521.         };
  522. /* If we start running low on DMA buffers, we abort the scatter-gather
  523.    operation, and free all of the memory we have allocated.  We want to
  524.    ensure that all scsi operations are able to do at least a non-scatter/gather
  525.    operation */
  526.         if(sgpnt[count].address == NULL){ /* Out of dma memory */
  527.           printk("Warning: Running low on SCSI DMA buffers");
  528.           /* Try switching back to a non scatter-gather operation. */
  529.           while(--count >= 0){
  530.             if(sgpnt[count].alt_address)
  531.               scsi_free(sgpnt[count].address, sgpnt[count].length);
  532.           };
  533.           this_count = SCpnt->request.current_nr_sectors;
  534.           buff = SCpnt->request.buffer;
  535.           SCpnt->use_sg = 0;
  536.           scsi_free(buff, SCpnt->sglist_len);
  537.           break;
  538.         };
  539.  
  540.         if (SCpnt->request.cmd == WRITE)
  541.           memcpy(sgpnt[count].address, sgpnt[count].alt_address,
  542.              sgpnt[count].length);
  543.           };
  544.         }; /* for loop */
  545.       };  /* Able to malloc sgpnt */
  546.     };  /* Host adapter capable of scatter-gather */
  547.  
  548. /* Now handle the possibility of DMA to addresses > 16Mb */
  549.  
  550.     if(SCpnt->use_sg == 0){
  551.       if (((int) buff) + (this_count << 9) > ISA_DMA_THRESHOLD &&
  552.         (SCpnt->host->unchecked_isa_dma)) {
  553.         buff = (char *) scsi_malloc(this_count << 9);
  554.         if(buff == NULL) panic("Ran out of DMA buffers.");
  555.         if (SCpnt->request.cmd == WRITE)
  556.           memcpy(buff, (char *)SCpnt->request.buffer, this_count << 9);
  557.       };
  558.     };
  559.  
  560. #ifdef DEBUG
  561.     printk("sd%d : %s %d/%d 512 byte blocks.\n", MINOR(SCpnt->request.dev),
  562.         (SCpnt->request.cmd == WRITE) ? "writing" : "reading",
  563.         this_count, SCpnt->request.nr_sectors);
  564. #endif
  565.  
  566.     cmd[1] = (SCpnt->lun << 5) & 0xe0;
  567.  
  568.     if (rscsi_disks[dev].sector_size == 1024){
  569.       if(block & 1) panic("sd.c:Bad block number requested");
  570.       if(this_count & 1) panic("sd.c:Bad block number requested");
  571.       block = block >> 1;
  572.       this_count = this_count >> 1;
  573.     };
  574.  
  575.     if (rscsi_disks[dev].sector_size == 256){
  576.       block = block << 1;
  577.       this_count = this_count << 1;
  578.     };
  579.  
  580.     if (((this_count > 0xff) ||  (block > 0x1fffff)) && rscsi_disks[dev].ten)
  581.         {
  582.         if (this_count > 0xffff)
  583.             this_count = 0xffff;
  584.  
  585.         cmd[0] += READ_10 - READ_6 ;
  586.         cmd[2] = (unsigned char) (block >> 24) & 0xff;
  587.         cmd[3] = (unsigned char) (block >> 16) & 0xff;
  588.         cmd[4] = (unsigned char) (block >> 8) & 0xff;
  589.         cmd[5] = (unsigned char) block & 0xff;
  590.         cmd[6] = cmd[9] = 0;
  591.         cmd[7] = (unsigned char) (this_count >> 8) & 0xff;
  592.         cmd[8] = (unsigned char) this_count & 0xff;
  593.         }
  594.     else
  595.         {
  596.         if (this_count > 0xff)
  597.             this_count = 0xff;
  598.  
  599.         cmd[1] |= (unsigned char) ((block >> 16) & 0x1f);
  600.         cmd[2] = (unsigned char) ((block >> 8) & 0xff);
  601.         cmd[3] = (unsigned char) block & 0xff;
  602.         cmd[4] = (unsigned char) this_count;
  603.         cmd[5] = 0;
  604.         }
  605.  
  606. /*
  607.  * We shouldn't disconnect in the middle of a sector, so with a dumb
  608.  * host adapter, it's safe to assume that we can at least transfer
  609.  * this many bytes between each connect / disconnect.
  610.  */
  611.  
  612.     SCpnt->transfersize = rscsi_disks[dev].sector_size;
  613.     SCpnt->underflow = this_count << 9;
  614.  
  615.     scsi_do_cmd (SCpnt, (void *) cmd, buff,
  616.              this_count * rscsi_disks[dev].sector_size,
  617.              rw_intr, SD_TIMEOUT, MAX_RETRIES);
  618. }
  619.  
  620. int check_scsidisk_media_change(int full_dev, int flag){
  621.     int retval;
  622.     int target;
  623.     struct inode inode;
  624.  
  625.     target =  DEVICE_NR(MINOR(full_dev));
  626.  
  627.     if (target >= NR_SD) {
  628.         printk("SCSI disk request error: invalid device.\n");
  629.         return 0;
  630.     };
  631.  
  632.     if(!rscsi_disks[target].device->removable) return 0;
  633.  
  634.     inode.i_rdev = full_dev;  /* This is all we really need here */
  635.     retval = sd_ioctl(&inode, NULL, SCSI_IOCTL_TEST_UNIT_READY, 0);
  636.  
  637.     if(retval){ /* Unable to test, unit probably not ready.  This usually
  638.              means there is no disc in the drive.  Mark as changed,
  639.              and we will figure it out later once the drive is
  640.              available again.  */
  641.  
  642.       rscsi_disks[target].device->changed = 1;
  643.       return 1; /* This will force a flush, if called from
  644.                check_disk_change */
  645.     };
  646.  
  647.     retval = rscsi_disks[target].device->changed;
  648.     if(!flag) rscsi_disks[target].device->changed = 0;
  649.     return retval;
  650. }
  651.  
  652. static void sd_init_done (Scsi_Cmnd * SCpnt)
  653. {
  654.   struct request * req;
  655.   struct task_struct * p;
  656.  
  657.   req = &SCpnt->request;
  658.   req->dev = 0xfffe; /* Busy, but indicate request done */
  659.  
  660.   if ((p = req->waiting) != NULL) {
  661.     req->waiting = NULL;
  662.     p->state = TASK_RUNNING;
  663.     if (p->counter > current->counter)
  664.       need_resched = 1;
  665.   }
  666. }
  667.  
  668. static int sd_init_onedisk(int i)
  669. {
  670.   int j = 0;
  671.   unsigned char cmd[10];
  672.   unsigned char *buffer;
  673.   char spintime;
  674.   int the_result, retries;
  675.   Scsi_Cmnd * SCpnt;
  676.  
  677. #ifdef DEBUG
  678.   printk ("sd_init_onedisk: disk %d, press mousebutton to continue\n", i);
  679.   waitbut();
  680. #endif
  681.  
  682.   /* We need to retry the READ_CAPACITY because a UNIT_ATTENTION is considered
  683.      a fatal error, and many devices report such an error just after a scsi
  684.      bus reset. */
  685.  
  686.   SCpnt = allocate_device(NULL, rscsi_disks[i].device->index, 1);
  687.   buffer = (unsigned char *) scsi_malloc(512);
  688.  
  689.   spintime = 0;
  690.  
  691.   /* Spin up drives, as required.  Only do this at boot time */
  692.   if (current == task[0]){
  693.     do{
  694.       cmd[0] = TEST_UNIT_READY;
  695.       cmd[1] = (rscsi_disks[i].device->lun << 5) & 0xe0;
  696.       memset ((void *) &cmd[2], 0, 8);
  697.       SCpnt->request.dev = 0xffff;  /* Mark as really busy again */
  698.       SCpnt->sense_buffer[0] = 0;
  699.       SCpnt->sense_buffer[2] = 0;
  700.  
  701.       scsi_do_cmd (SCpnt,
  702.            (void *) cmd, (void *) buffer,
  703.            512, sd_init_done,  SD_TIMEOUT,
  704.            MAX_RETRIES);
  705.  
  706.       while(SCpnt->request.dev != 0xfffe);
  707.  
  708.       the_result = SCpnt->result;
  709.  
  710.       /* Look for non-removable devices that return NOT_READY.    Issue command
  711.      to spin up drive for these cases. */
  712.       if(the_result && !rscsi_disks[i].device->removable &&
  713.      SCpnt->sense_buffer[2] == NOT_READY) {
  714.     int time1;
  715.     if(!spintime){
  716.       printk( "sd%d: Spinning up disk...", i );
  717.       cmd[0] = START_STOP;
  718.       cmd[1] = (rscsi_disks[i].device->lun << 5) & 0xe0;
  719.       cmd[1] |= 1;    /* Return immediately */
  720.       memset ((void *) &cmd[2], 0, 8);
  721.       cmd[4] = 1; /* Start spin cycle */
  722.       SCpnt->request.dev = 0xffff;    /* Mark as really busy again */
  723.       SCpnt->sense_buffer[0] = 0;
  724.       SCpnt->sense_buffer[2] = 0;
  725.  
  726.       scsi_do_cmd (SCpnt,
  727.                (void *) cmd, (void *) buffer,
  728.                512, sd_init_done,  SD_TIMEOUT,
  729.                MAX_RETRIES);
  730.  
  731.       while(SCpnt->request.dev != 0xfffe);
  732.  
  733.       spintime = jiffies;
  734.     };
  735.  
  736.     time1 = jiffies;
  737.     while(jiffies < time1 + 100); /* Wait 1 second for next try */
  738.     printk( "." );
  739.       };
  740.     } while(the_result && spintime && spintime+5000 > jiffies);
  741.     if (spintime) {
  742.        if (the_result)
  743.            printk( "not responding...\n" );
  744.        else
  745.            printk( "ready\n" );
  746.     }
  747.   };  /* current == task[0] */
  748.  
  749.  
  750.   retries = 3;
  751.   do {
  752.     cmd[0] = READ_CAPACITY;
  753.     cmd[1] = (rscsi_disks[i].device->lun << 5) & 0xe0;
  754.     memset ((void *) &cmd[2], 0, 8);
  755.     memset ((void *) buffer, 0, 8);
  756.     SCpnt->request.dev = 0xffff;  /* Mark as really busy again */
  757.     SCpnt->sense_buffer[0] = 0;
  758.     SCpnt->sense_buffer[2] = 0;
  759.  
  760.     scsi_do_cmd (SCpnt,
  761.          (void *) cmd, (void *) buffer,
  762.          8, sd_init_done,  SD_TIMEOUT,
  763.          MAX_RETRIES);
  764.  
  765.     if (current == task[0])
  766.       while(SCpnt->request.dev != 0xfffe);
  767.     else
  768.       if (SCpnt->request.dev != 0xfffe){
  769.     SCpnt->request.waiting = current;
  770.     current->state = TASK_UNINTERRUPTIBLE;
  771.     while (SCpnt->request.dev != 0xfffe) schedule();
  772.       };
  773.  
  774.     the_result = SCpnt->result;
  775.     retries--;
  776.  
  777.   } while(the_result && retries);
  778.  
  779.   SCpnt->request.dev = -1;  /* Mark as not busy */
  780.  
  781.   wake_up(&scsi_devices[SCpnt->index].device_wait);
  782.  
  783.   /* Wake up a process waiting for device*/
  784.  
  785.   /*
  786.    *    The SCSI standard says "READ CAPACITY is necessary for self confuring software"
  787.    *    While not mandatory, support of READ CAPACITY is strongly encouraged.
  788.    *    We used to die if we couldn't successfully do a READ CAPACITY.
  789.    *    But, now we go on about our way.  The side effects of this are
  790.    *
  791.    *    1.  We can't know block size with certainty.  I have said "512 bytes is it"
  792.    *        as this is most common.
  793.    *
  794.    *    2.  Recovery from when some one attempts to read past the end of the raw device will
  795.    *        be slower.
  796.    */
  797.  
  798.   if (the_result)
  799.     {
  800.       printk ("sd%d : READ CAPACITY failed.\n"
  801.           "sd%d : status = %x, message = %02x, host = %d, driver = %02x \n",
  802.           i,i,
  803.           status_byte(the_result),
  804.           msg_byte(the_result),
  805.           host_byte(the_result),
  806.           driver_byte(the_result)
  807.           );
  808.       if (driver_byte(the_result)  & DRIVER_SENSE)
  809.     printk("sd%d : extended sense code = %1x \n", i, SCpnt->sense_buffer[2] & 0xf);
  810.       else
  811.     printk("sd%d : sense not available. \n", i);
  812.  
  813.       printk("sd%d : block size assumed to be 512 bytes, disk size 1GB.  \n", i);
  814.       rscsi_disks[i].capacity = 0x1fffff;
  815.       rscsi_disks[i].sector_size = 512;
  816.  
  817.       /* Set dirty bit for removable devices if not ready - sometimes drives
  818.      will not report this properly. */
  819.       if(rscsi_disks[i].device->removable &&
  820.      SCpnt->sense_buffer[2] == NOT_READY)
  821.     rscsi_disks[i].device->changed = 1;
  822.  
  823.     }
  824.   else
  825.     {
  826.       /* record capacity; add one since last logical block address
  827.      is retur`ned */
  828.       rscsi_disks[i].capacity = ((buffer[0] << 24) |
  829.     (buffer[1] << 16) |
  830.       (buffer[2] << 8) |
  831.         buffer[3]) + 1;
  832.  
  833.       rscsi_disks[i].sector_size = (buffer[4] << 24) |
  834.     (buffer[5] << 16) | (buffer[6] << 8) | buffer[7];
  835.  
  836.       if (rscsi_disks[i].sector_size != 512 &&
  837.       rscsi_disks[i].sector_size != 1024 &&
  838.       rscsi_disks[i].sector_size != 256)
  839.     {
  840.       printk ("sd%d : unsupported sector size %d.\n",
  841.           i, rscsi_disks[i].sector_size);
  842.       if(rscsi_disks[i].device->removable){
  843.         rscsi_disks[i].capacity = 0;
  844.       } else {
  845.         printk ("scsi : deleting disk entry.\n");
  846.         for  (j=i;  j < NR_SD - 1;)
  847.           rscsi_disks[j] = rscsi_disks[++j];
  848.         --i;
  849.         --NR_SD;
  850.         scsi_free(buffer, 512);
  851.         return i;
  852.       };
  853.     }
  854.       if(rscsi_disks[i].sector_size == 1024)
  855.     rscsi_disks[i].capacity <<= 1;    /* Change this into 512 byte sectors */
  856.       if(rscsi_disks[i].sector_size == 256)
  857.     rscsi_disks[i].capacity >>= 1;    /* Change this into 512 byte sectors */
  858.     }
  859.  
  860. #ifdef DEBUG
  861.       printk ("disk %d has capacity %d, sector size %d\n", i,
  862.           rscsi_disks[i].capacity, rscsi_disks[i].sector_size);
  863. #endif
  864.  
  865.   rscsi_disks[i].ten = 1;
  866.   rscsi_disks[i].remap = 1;
  867.   scsi_free(buffer, 512);
  868.   return i;
  869. }
  870.  
  871. /*
  872.     The sd_init() function looks at all SCSI drives present, determines
  873.     their size, and reads partition table entries for them.
  874. */
  875.  
  876. unsigned long sd_init(unsigned long memory_start, unsigned long memory_end)
  877. {
  878.     int i;
  879.  
  880.     if (register_blkdev(MAJOR_NR,"sd",&sd_fops)) {
  881.         printk("Unable to get major %d for SCSI disk\n",MAJOR_NR);
  882.         return memory_start;
  883.     }
  884.     if (MAX_SD == 0) return memory_start;
  885.  
  886.     sd_sizes = (int *) memory_start;
  887.     memory_start += (MAX_SD << 4) * sizeof(int);
  888.     memset(sd_sizes, 0, (MAX_SD << 4) * sizeof(int));
  889.  
  890.     sd_blocksizes = (int *) memory_start;
  891.     memory_start += (MAX_SD << 4) * sizeof(int);
  892.     for(i=0;i<(MAX_SD << 4);i++) sd_blocksizes[i] = 1024;
  893.     blksize_size[MAJOR_NR] = sd_blocksizes;
  894.  
  895.     sd = (struct hd_struct *) memory_start;
  896.     memory_start += (MAX_SD << 4) * sizeof(struct hd_struct);
  897.  
  898.     sd_gendisk.max_nr = MAX_SD;
  899.     sd_gendisk.part = sd;
  900.     sd_gendisk.sizes = sd_sizes;
  901.     sd_gendisk.real_devices = (void *) rscsi_disks;
  902.  
  903.     for (i = 0; i < NR_SD; ++i)
  904.       i = sd_init_onedisk(i);
  905.  
  906.     blk_dev[MAJOR_NR].request_fn = DEVICE_REQUEST;
  907.  
  908.     /* If our host adapter is capable of scatter-gather, then we increase
  909.        the read-ahead to 16 blocks (32 sectors).  If not, we use
  910.        a two block (4 sector) read ahead. */
  911.     if(rscsi_disks[0].device->host->sg_tablesize)
  912.       read_ahead[MAJOR_NR] = 32;
  913.     /* 64 sector read-ahead */
  914.     else
  915.       read_ahead[MAJOR_NR] = 4;  /* 4 sector read-ahead */
  916.  
  917.     sd_gendisk.next = gendisk_head;
  918.     gendisk_head = &sd_gendisk;
  919.     return memory_start;
  920. }
  921.  
  922. unsigned long sd_init1(unsigned long mem_start, unsigned long mem_end){
  923.   rscsi_disks = (Scsi_Disk *) mem_start;
  924.   mem_start += MAX_SD * sizeof(Scsi_Disk);
  925.   return mem_start;
  926. };
  927.  
  928. void sd_attach(Scsi_Device * SDp){
  929.   rscsi_disks[NR_SD++].device = SDp;
  930.   if(NR_SD > MAX_SD) panic ("scsi_devices corrupt (sd)");
  931. };
  932.  
  933. #define DEVICE_BUSY rscsi_disks[target].device->busy
  934. #define USAGE rscsi_disks[target].device->access_count
  935. #define CAPACITY rscsi_disks[target].capacity
  936. #define MAYBE_REINIT  sd_init_onedisk(target)
  937. #define GENDISK_STRUCT sd_gendisk
  938.  
  939. /* This routine is called to flush all partitions and partition tables
  940.    for a changed scsi disk, and then re-read the new partition table.
  941.    If we are revalidating a disk because of a media change, then we
  942.    enter with usage == 0.  If we are using an ioctl, we automatically have
  943.    usage == 1 (we need an open channel to use an ioctl :-), so this
  944.    is our limit.
  945.  */
  946. int revalidate_scsidisk(int dev, int maxusage){
  947.       int target, major;
  948.       struct gendisk * gdev;
  949.       int max_p;
  950.       int start;
  951.       int i;
  952.  
  953.       target =  DEVICE_NR(MINOR(dev));
  954.       gdev = &GENDISK_STRUCT;
  955.  
  956.       cli();
  957.       if (DEVICE_BUSY || USAGE > maxusage) {
  958.         sti();
  959.         printk("Device busy for revalidation (usage=%d)\n", USAGE);
  960.         return -EBUSY;
  961.       };
  962.       DEVICE_BUSY = 1;
  963.       sti();
  964.  
  965.       max_p = gdev->max_p;
  966.       start = target << gdev->minor_shift;
  967.       major = MAJOR_NR << 8;
  968.  
  969.       for (i=max_p - 1; i >=0 ; i--) {
  970.         sync_dev(major | start | i);
  971.         invalidate_inodes(major | start | i);
  972.         invalidate_buffers(major | start | i);
  973.         gdev->part[start+i].start_sect = 0;
  974.         gdev->part[start+i].nr_sects = 0;
  975.       };
  976.  
  977. #ifdef MAYBE_REINIT
  978.       MAYBE_REINIT;
  979. #endif
  980.  
  981.       gdev->part[start].nr_sects = CAPACITY;
  982.       resetup_one_dev(gdev, target);
  983.  
  984.       DEVICE_BUSY = 0;
  985.       return 0;
  986. }
  987.